<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user
 *
 * @author Willie
 */
class user extends CI_Model {
    private $userTable = 'user_login';
    private $cost=10;
    private $educationTable="user_education";
    private $majorTable="majors";
    private $degreeTable="edu_degree";
    private $schoolTable="schools";
    private $userCertificate="user_certificates";
    private $certificate="certificates";
    
    function __construct() {
        parent::__construct();
    }
 
    
    function getUser($uid=NULL){
        if($uid==NULL){
            return NULL;
        }
        $sql = "SELECT * FROM ".$this->userTable." WHERE user_login.id=".$uid;
        $user =$this->db->query($sql);
        foreach($user->result() as $u){
            return $u;
        }
    }
    function addUser($email,$password,$lastname,$firstname){
        $email_esc = $this->db->escape_str($email);
        $password_esc=$this->db->escape_str($password);
        $firstname_esc=$this->db->escape_str($firstname);
        $lastname_esc=$this->db->escape_str($lastname);
        $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
        $salt = sprintf("$2a$%02d$", $this->cost) . $salt;
        $hash = crypt($password_esc, $salt);
        $data = array('PASSWORD'=>$hash,
            'EMAIL_ADDR'=>$email_esc,
            'FST_NAME'=>$firstname_esc,
            'LAST_NAME'=>$lastname_esc);
        $result=$this->db->insert($this->userTable,$data);
        
        return array("success"=>$result,"id"=>$this->db->insert_id());//$result;
        
    }

    function validateUser($email,$password,$includeUser=FALSE){
        $resp=array();
        if($email==NULL && $password==NULL){
            return array("success"=>FALSE,"message"=>"Invalid email and password") ;
        }
        $curUser =$this->checkEmailExist($email,TRUE);
        if($curUser==NULL){
           return array("success"=>FALSE,"message"=>"Email does not exist");
        }
        $dbPwd = $curUser->PASSWORD;// get the password
        if(crypt($password,$dbPwd)==$dbPwd){
            // valid user
            $resp =array("success"=>TRUE,"message"=>"you have been validated");
            if($includeUser){
                $resp['user']=$curUser;
            }
            return $resp;
        }else{
            return array("success"=>FALSE,"message"=>"Your email and password does not match!");
        }
    }
    
    function checkEmailExist($email,$retUser=FALSE){
        $sql="SELECT * FROM ".$this->userTable." WHERE EMAIL_ADDR='$email'";
        $user = $this->db->query($sql);
        $curUser =NULL;
        foreach($user->result() as $u){
            $curUser=$u;
            break;
        }
        if($retUser!==FALSE){
            return $curUser;
        }
        return $curUser !=NULL;// if user not null that mean email exist else email doesn't exist
    }
    
    function getEducationByUid($uid){
        if($uid!=NULL && $uid!=""){
            $sql="SELECT eu.id, DEGREE_NAME,SCHOOL_NAME,MAJOR_NAME FROM ".$this->educationTable. " AS eu LEFT JOIN";
            $sql.=" ".$this->schoolTable." AS s ON eu.SCHOOL_ID = s.id LEFT JOIN ".$this->majorTable." AS m ON eu.MAJOR_ID";
            $sql.=" = m.id LEFT JOIN ".$this->degreeTable." AS d ON eu.DEGREE_ID = d.id WHERE USER_ID=".$uid;
            $rs= $this->db->query($sql);
            if($rs!=NULL){
                return $rs->result();
            }
            
        }
        return NULL;
    }
    
    function getCertificatesByUid($uid){
        if($uid!=NULL && $uid!=""){
            $sql="SELECT uc.id, CERTIFICATES_NAME, ACQUIRED_DATE FROM ".$this->userCertificate. " AS uc LEFT JOIN";
            $sql.=" ".$this->certificate." AS c ON uc.CERTIFICATE_ID=c.id WHERE uc.USER_ID=".$uid;
            $rs= $this->db->query($sql);
            if($rs!=NULL){
                return $rs->result();
            }
            
        }
        return NULL;
    }
    
    function addMessage($uid,$subject,$body,$read){
        $sql = "INSERT INTO messages (TO_USER,MSG_SUBJ,MSG_BODY,MSG_SENT_TIME,MSG_READ_YN) VALUES ".
                "($uid,'$subject','$body',now(),'$read')";
        log_message('error',$sql);
        return $this->db->simple_query($sql);
        
                
        
    }
    
}
